package net;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import visual.FrameHandler;

public class GameClient implements Runnable{
	Socket socket;
	public Thread clientThread;
	public static String ip;
    public ObjectOutputStream oos;
    public ObjectInputStream ois;
	public int clientId;
    
	public GameClient(String _ip){
		ip = _ip;
		clientThread = new Thread(this);
		clientThread.start();
	}

	public void sendMessage(Message _msg){
		try{
			oos.writeObject(_msg);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void receiveMessage(Message _msg){
		FrameHandler.handleMessage(_msg);
	}
	
	public void run(){
		while(socket == null){
			try{
				socket = new Socket(ip, 9999);
				oos = new ObjectOutputStream(socket.getOutputStream());
				ois = new ObjectInputStream(socket.getInputStream());
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		Message tmp;
		try {
			while(null != (tmp = (Message)ois.readObject())){
				receiveMessage(tmp);
				Thread.yield();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
